/**
 * Copyright (C) 2011 ChangSha Creator Science & Technology CO,.LTD. All rights reserved.
 * Creator PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package wzw.swing.table;

import java.awt.Color;
import java.awt.Component;
import java.util.EventObject;

import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.text.JTextComponent;

/**
 * <p>java class file describe</p>
 * @author Zeven on 2011-7-19
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @since	JDK5
 */

public class PlainTable extends JTable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * <p>java class file describe</p>
	 * @author Zeven on 2011-7-19
	 * @see		Object#equals(java.lang.Object)
	 */

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public boolean editCellAt(int row, int column, EventObject e) {
		boolean result = super.editCellAt(row, column, e);
		final Component editor = getEditorComponent();
		if (editor != null && editor instanceof JTextComponent) {
			if (e == null) {
				((JTextComponent) editor).selectAll();
			} else {
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						((JTextComponent) editor).selectAll();
					}
				});
			}
		}
		return result;
	}
	
	private PlainTableModel plainTableModel = null;
    public void setModel(PlainTableModel plainTableModel) {
    	this.plainTableModel = plainTableModel;
    	super.setModel( plainTableModel );
    }
    
    public PlainTableModel getModel(){
    	return this.plainTableModel;
    }

    
    
	public PlainTable() {
		super();
		this.setRowHeight(21); //设置默认表行高（如果是EXCL，行高一般是20-25，列宽根据需要调整了，字号一般是14号）
	    this.setGridColor(Color.LIGHT_GRAY); //改变默认风格
	}

	/**
	 * 
	 * // 设置JTable斑马线效果
	 * 	// 设置斑马线开始很迷惑，一直认为斑马线应该是按照行去设置的，但是确实按照列去设置的，想了一会想明白了，
	 * 	// 看上面重写的getTableCellRendererComponent方法中，是按照某列每一行进行设置的，在每一列中是斑马线的效果，
	 * 	// 然后按照上面的for循环，给每一列加上这个渲染器，由于所有列向同行的设置（颜色）是相同的，所以最后才出现斑马线
	 * 	// 的效果
	 */
	public void showZebraCross() {
		// 斑马线设置
		DefaultTableCellRenderer d = new ZebraCrossTableCellRenderer();
		
		for (int i = 0; i < this.getColumnCount(); i++) {
			TableColumn col = this.getColumn(this.getColumnName(i));
//			TableCellRenderer tcr = col.getCellRenderer();
			col.setCellRenderer(d);// 将斑马线对象放置到表格中
		}
	}
}


